<!DOCTYPE html>
<!--
 *  创建时间: 2020-08-05-19:21
 *  作   者: lpc
 *  Email : lpc@hll520.cn
-->
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="description" content="">
    <meta name="author" content="">
    <title>购物车</title>
    <!-- JQuery -->
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <!-- Bootstrap核心CSS和JS -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <!-- 自定义JS -->
    <script type="text/javascript">
        $(function () {

            /* 获取用户信息 */
            $.getJSON(
                "/user/getInfo",
                {},
                function (json) {
                    if (json.statusCode !== 200) {

                    } else {
                        $("#nav_login").html(`
                        <a href="/space">
                        <span class="glyphicon glyphicon-user"></span>
                        <span id="userName">${json.data.realname}</span></a>
                        `);

                        $("#nav_register").html(`
                    <a href="/user/signOut">安全退出</a>
                    `);

                    }
                },
                "json"
            );

            /* 获取订单详情 */
            $.getJSON(
                "/shopping/list/getData",
                {},
                function (json) {
                    if (json.statusCode !== 200) {
                        alert("购物车还是空的哦！" + json.msg);
                        location.href = "/home"
                        return false;
                    }
                    let shopList = json.data;
                    let priceAll = 0;
                    let shopSum= shopList.length
                    for (let i = 0; i < shopList.length; i++) {

                        let shop = shopList[i];
                        let theSum = shop.goodsSum;
                        let thePrice = shop.goods.price / 100;
                        let price = theSum * thePrice;
                        /* 赋值 */
                        priceAll += price;

                        if (shop.goods.image == null || shop.goods.image === "")
                            shop.goods.image = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596716481337&di=fab03d71304d4827ecde2e1031b53a8e&imgtype=0&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Ffc1f4134970a304e9b7f6625d4c8a786c8175c94.jpg";


                        /* 显示清单 */
                        $("#shop_info").append(`
                        <tr id="${shop.shoppingId}">
                        <td><img src="${shop.goods.image}" width="80px" alt="预览图"/></td>
                        <td>${shop.goods.name}</td>
                        <td>${shop.goods.price / 100}</td>
                        <td>
                            <a href="javascript:editSum(${shop.shoppingId},${shop.goodsSum}-1)"><b>-</b></a>&nbsp;
                            ${shop.goodsSum} &nbsp;
                              <a href="javascript:editSum(${shop.shoppingId},${shop.goodsSum}+1)"><b>+</b></a>
                        </td>
                        <td>${price}</td>
                        <td>${shop.goods.updateTime}</td>
                        <td><a href="javaScript:del(${shop.shoppingId})">移除</a></td>
                        </tr>
                        `);
                    }
                    /* 设置总价格时间和订单编号 */
                    $("#price").html(priceAll);
                    $("#sum").html(shopSum);
                }
            );


            /* 生成订单 */
            $("#btn_go").click(function () {
                $.getJSON(
                    "/shopping/submitShop",
                    {},
                    function (json) {
                        if (json.statusCode !== 200) {
                            if (json.data !== null) {
                                $("#" + json.data.shoppingId).css("background", "aquamarine");
                                alert("提交失败" + json.msg);
                            }
                            alert(json.msg);
                            return false;
                        }
                        alert("提交成功");
                        location.href = "/order/info/" + json.data;
                    }
                )
            });


        });


        /* 移除 */
        function del(goods) {
            if (!confirm("确定移除吗？"))
                return false;
            $.getJSON(
                "/shopping/delShop",
                {shopID: goods},
                function (json) {
                    if (json.statusCode !== 200) {
                        alert("移除失败！" + json.msg);
                        return false;
                    }
                    location.reload();
                }
            )
        }

        /* 修改数量 */
        function editSum(shop, sum) {
            if(sum<1){
                alert("不能低于一件哦！");
                return false;
            }
            $.getJSON(
                "/shopping/edit",
                {shopId:shop,number:sum},
                function (json) {
                    if (json.statusCode === 302) {
                        alert(json.msg);
                        location.href=json.data;
                        return false;
                    }else if(json.statusCode!==200){
                        alert("修改失败！" + json.msg);
                        return false;
                    }
                    location.reload();
                }
            )

        }

        /* 搜索 */
        function search() {
            let key = $("#key").val();
            location.href = "/goods/search/" + key;
        }
    </script>
</head>
<body>
<div class="container">

    <!-- 导航 -->
    <nav class="navbar navbar-default" role="navigation">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1"><span
                    class="sr-only">WTUShop导航栏</span><span class="icon-bar"></span><span
                    class="icon-bar"></span><span class="icon-bar"></span></button>
            <a class="navbar-brand" href="/">WTUShop</a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <!-- 左边 -->
            <ul class="nav navbar-nav">
                <li><a href="/goods/search">商场</a></li>
                <li><a href="/kill/list">秒杀活动</a></li>
                <li><a href="/space/">个人中心</a></li>
                <li><a href="/admin/manage">控制台</a></li>
            </ul>
            <!-- 右边 -->
            <ul class="nav navbar-nav navbar-right">
                <li class="active"><a href="/shopping/list">购物车</a></li>
                <li><a href="/order/list">订单查询</a></li>
                <li id="nav_login"><a href="/login"><span
                        class="glyphicon glyphicon-user"></span><span id="userName">请登录</span></a></li>
                <li id="nav_register"><a href="/register">注册</a></li>
                <li>&nbsp;&nbsp;</li><!-- 占位符 -->
            </ul>
        </div>

    </nav>
    <!-- 标题和搜索 -->
    <div class="row clearfix page-header">
        <div class="col-md-8 column">
            <a href="/"><h1>WTUShop</h1></a>
        </div>

        <div class="input-group col-md-4 column " style="margin-top: 20px">
            <input type="text" class="form-control" placeholder="搜索商品" id="key">
            <span class="input-group-btn">
                <a href="javaScript:search()">
                <button id="btn_search" class="btn btn-default">
                    <span class="glyphicon glyphicon-search"></span> 搜索
                </button>
                    </a>
                    &nbsp;
            </span>
        </div>
    </div>


    <div class="row clearfix">


        <div class="col-md-12 column">
            <ul class="breadcrumb">
                <li><a href="/home">商场</a></li>
                <li><a href="/space">个人中心</a></li>
                <li class="active">购物车</li>
            </ul>


            <div class="row clearfix">
                <div class="col-md-6 column">
                    <h3>
                        商品共：<span style="color: cornflowerblue;" id="sum">X</span>类
                    </h3>
                </div>
                <div class="col-md-6 column text-right">
                    <h3>
                        总价值：￥<span style="color: firebrick;" id="price">X</span>
                    </h3>
                </div>
            </div>


            <table class="table table-hover table-bordered table-striped">
                <thead>
                <tr>
                    <th>预览</th>
                    <th>商品</th>
                    <th>单价</th>
                    <th>数量</th>
                    <th>小计</th>
                    <th>添加时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="shop_info">


                </tbody>
            </table>


            <div class="row clearfix">
                <div class="col-md-4 column">

                </div>

                <div class="col-md-4 column text-right">

                </div>

                <div class="col-md-4 column text-bottom">
                    <button type="button" class="btn btn-default btn-block btn-primary" id="btn_go">
                        生成订单
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>